package com.lxyk.service.statistic.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lxyk.dao.statistic.ClassAttendanceDao;
import com.lxyk.dao.statistic.StuAttendanceDao;
import com.lxyk.dao.user.ClassesDao;
import com.lxyk.domain.*;
import com.lxyk.service.statistic.StuAttendanceService;
import com.lxyk.utils.StatisticUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

//学生到课率
@Service
public class StuAttendanceServiceImpl implements StuAttendanceService {
    @Autowired
    private StuAttendanceDao stuAttendanceDao;
    @Autowired
    private ClassAttendanceDao classAttendanceDao;
    @Autowired
    private ClassesDao classesDao;
    //班级某一天所有学生到课率
    @Override
    public List<StuStatisticVO> findClassDayAttendance(Integer cid, Date date) {
//        Integer[] yearAndMonth = StatisticUtils.getYearAndMonth(date);
        date = date!=null?date:new Date();
        List<StuStatisticVO> data = stuAttendanceDao.findClassDayAttendance(cid, date);
        return data;
    }

    //分页查询:班级某一天所有学生到课率
    @Override
    public PageInfo<StuStatisticVO> findClassDayAttendance(Integer cid, Date date, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
//        Integer[] yearAndMonth = StatisticUtils.getYearAndMonth(date);
        date = date!=null?date:new Date();
        List<StuStatisticVO> data = stuAttendanceDao.findClassDayAttendance(cid, date);
        PageInfo<StuStatisticVO> pageInfo = new PageInfo<>(data);
        return pageInfo;
    }

    //条件查询
    @Override
    public PageInfo<StuStatisticVO> findByCondition(StuStatisticVO stuStatisticVO,Integer pageNum,Integer pageSize) {
        pageNum = pageNum!=null?pageNum:1;
        pageSize = pageSize!=null?pageSize:10;
        PageHelper.startPage(pageNum,pageSize);
        List<StuStatisticVO> data = stuAttendanceDao.findByCondition(stuStatisticVO);
        PageInfo<StuStatisticVO> pageInfo = new PageInfo<>(data);
        return pageInfo;
    }

    //更新到课率(批量)
    @Override
//    @Transactional
    public void save(Integer cid ,Date recordDate,List<StuStatisticVO> list) {
        if(list.get(0).getId()!=null){
            //修改当前班级学生到课率
            stuAttendanceDao.update(list);
//            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
//            if(!sdf.format(oldDate).equals(sdf.format(recordDate))){
//                //删除原来数据
//                classAttendanceDao.del(cid,oldDate);
//            }
        }else{
            stuAttendanceDao.add(list);
        }
        //修改统计数据
        //1 重新计算当前班级,当前年/月/日的到课率
        int realCount = stuAttendanceDao.findRealAttendanceCount(cid, recordDate);//1
//        int allCount = stuAttendanceDao.findAllAttendanceCount(cid,recordDate);
        Classes classes = classesDao.findById(cid);
        Integer allCount = classes.getEffeNumber();//有效人数  60
        double avgAttendance = Double.valueOf(realCount)/allCount;  //1/60
        avgAttendance = Double.valueOf(String.format("%.2f",avgAttendance))*100;
        System.out.println("实际到课人数:"+realCount);
        System.out.println("应到人数:"+allCount);
        System.out.println("平均到课率是:"+avgAttendance);
        //查询当前班级日期是否有统计数据
        ClassAttendance classAttendance = classAttendanceDao.findByClassAndDate(cid, recordDate);
        if(classAttendance!=null){
            //有数据:修改
            //2 修改统计数据
            classAttendanceDao.updateAttendance(cid,recordDate,avgAttendance);
        }else{
            //没有数据
            //3 添加统计数据
            classAttendanceDao.addAttendance(cid,recordDate,avgAttendance);
        }
    }

    //根据班级id和日期查询学生信息
    @Override
    public PageInfo<StuStatisticVO> findStudentByCidAndDate(Integer cid, Date recordDate, Integer pageNum, Integer pageSize) {
        //只查询没有添加到课率的学生
        pageNum = pageNum!=null?pageNum:1;
        pageSize = pageSize!=null?pageSize:10;
        PageHelper.startPage(pageNum,pageSize);
        List<StuStatisticVO> list = stuAttendanceDao.findStudentByCidAndDate(cid, recordDate);
        return new PageInfo<>(list);
    }

    @Override
    public Date getLastDate(Integer cid) {
        return stuAttendanceDao.getLastDate(cid);
    }

}
